const formValidateFun = (value: string | number, type: string): string | number => {
  value = (value + "").replace(/(^\s*)|(\s*$)/g, ""); // 先去除前后空格，排除都是空格的情况
  switch (type) {
    case "number":
      value = value ? Number(value) : "";
      break;
  }
  return value;
};

interface FormRule {
  required?: boolean;
  message?: string;
  trigger?: string | string[];
  min?: number;
  max?: number;
  type?: string;
  pattern?: RegExp;
  transform?: (value: any) => any;
  validator?: (rule: any, value: any, callback: (error?: Error) => void) => void;
}

const validate = {
  //必填
  required: (trigger = "blur"): FormRule[] => {
    return [
      {
        required: true,
        message: "不能为空！",
        trigger: [trigger]
      }
    ];
  },
  //数组必填
  arrayRequired: (): FormRule[] => {
    return [
      {
        validator: (_rule: any, value: any, callback: (error?: Error) => void) => {
          if (value && value.length === 0) {
            callback(new Error("不能为空！"));
          } else {
            callback();
          }
        },
        trigger: ["blur"]
      },
      {
        required: true,
        message: "不能为空!",
        trigger: ["blur"]
      }
    ];
  },
  //填写字符长度
  maxLength: (required = false, min = 0, max = 255): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        min: min,
        message: `不能小于${min}位字符！`,
        trigger: "blur"
      },
      {
        max: max,
        message: `不能大于${max}位字符！`,
        trigger: "blur"
      }
    ];
  },
  //数字
  number: (required = true): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        validator: (_rule: any, value: any, callback: (error?: Error) => void) => {
          const reg = /^[0-9.]*$/.test(value);
          if (!reg) {
            callback(new Error("必须为数字"));
          } else {
            callback();
          }
        },
        trigger: ["blur"]
      }
    ];
  },
  // 数字范围
  numRange: (required = false, min = 0, max = 999): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        min: min,
        max: max,
        type: "number",
        message: `数字需在${min}-${max}之间`,
        transform: (value: any) =>
          formValidateFun(value, "number")
      }
    ];
  },
  //小数
  decimal: (required = true, num = 2): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        pattern: /^[0-9.]*$/,
        message: "必须为数字",
        trigger: "blur"
      },
      {
        pattern: new RegExp(
          "^[0-9]+[.]?[0-9]{0," + num + "}$"
        ),
        message: `输入最多${num}位小数`,
        trigger: "blur"
      }
    ];
  },
  //姓名
  realName: (required = false): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        max: 15,
        message: `不能大于15位字符！`,
        trigger: "blur"
      },
      {
        pattern: new RegExp(
          "^(([a-zA-Z+\\.?\\·?a-zA-Z+]{2,15}$)|([\u4e00-\u9fa5+\\·?\u4e00-\u9fa5+]{2,15}$))"
        ),
        message: `请输入正确姓名!`,
        trigger: "blur"
      }
    ];
  },
  //身份证
  IDcode: (required = false): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        pattern:
          /^(^[1-9]\d{5}[1-9]\d{3}(((0[2])([0|1|2][0-8])|(([0-1][1|4|6|9])([0|1|2][0-9]|[3][0]))|(((0[1|3|5|7|8])|(1[0|2]))(([0|1|2]\d)|3[0-1]))))((\d{4})|\d{3}[Xx])$)$/,
        message: `请输入正确身份证号!`,
        trigger: "blur"
      }
    ];
  },
  //手机号
  phone: (required = false): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        validator: (_rule: any, value: any, callback: (error?: Error) => void) => {
          const reg =
            /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/;
          if (reg.test(value)) {
            callback();
          } else {
            callback(new Error("请输入正确手机号"));
          }
        },
        trigger: ["blur"]
      }
    ];
  },
  //密码
  password: (required = false, min = 8, max = 18): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        pattern: new RegExp(
          `^(?=.*?[a-zA-Z])(?=.*?[0-9])(?=.*?[_\\-@$#*^%&~!=])[a-zA-Z0-9_\\-@$#*^%&~!=]{${min},${max}}$`
        ),
        message: `密码必须由字母、数字和特殊字符三种组成的${min}-${max}位字符`,
        trigger: "blur"
      }
    ];
  },
  // 邮箱
  email: (required = false): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        max: 50,
        message: `不能大于50位字符！`,
        trigger: "blur"
      },
      {
        pattern:
          /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
        message: `请输入正确的电子邮箱!`,
        trigger: "blur"
      }
    ];
  },
  // 社会统一信用代码
  societyCode: (required = false): FormRule[] => {
    return [
      {
        required: required,
        message: "不能为空!",
        trigger: ["blur"]
      },
      {
        pattern:
          /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/,
        message: `请输入正确社会统一信用代码!`,
        trigger: "blur"
      }
    ];
  },

  checkValidate: (formRef: any): Promise<any> => {
    return new Promise((resolve, reject) => {
      formRef.validate((valid: boolean) => {
        if (valid) {
          resolve(valid);
        } else {
          reject(valid);
        }
      });
    });
  }
};

export default validate;
